package com.echoesnet.echoesshortvideo.Repositories;

import com.echoesnet.echoesshortvideo.Api.HttpMethods;
import com.echoesnet.echoesshortvideo.Api.RequestHeader;
import com.echoesnet.echoesshortvideo.Beans.UserBean;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Utils.Cryptology.EncryptSHA1;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import io.reactivex.Observer;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 15:56
 * @description
 */
//@Singleton  // informs Dagger that this class should be constructed once
public class RegisterRepository
{
    /**
     * 注册
     * @param password

     * @param observer
     */
    public void register(String phone,String password,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        try
        {
            reqMap.put(RequestHeader.PASSWORD, EncryptSHA1.SHA1(password));
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        reqMap.put(RequestHeader.PHONE, phone);
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_REGISTER, reqMap, true);
    }

    /**
     * 发验证码
     * @param phone
     * @param channel  "来源 0注册1找回密码 3绑定手机号 4更改绑定手机号"
     * @param observer
     */
    public void sendVerificationCode(String phone, String  channel,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phone);
        reqMap.put(RequestHeader.VIDEO_SOURCE, channel);
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_SENDCODES, reqMap, true);
    }

    /**
     * 发语音验证码
     * @param phone
     * @param channel  "来源 0注册1找回密码 3绑定手机号 4更改绑定手机号"
     * @param observer
     */
    public void sendVoiceCode(String phone, String  channel,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phone);
        reqMap.put(RequestHeader.VIDEO_SOURCE, channel);
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_SENDVOICECODE, reqMap, true);
    }

    /**
     * 验证验证码
     * @param verifyCode
     */
    public void checkVerifyCode(String phone,String verifyCode,String channel,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phone);
        reqMap.put(RequestHeader.CODE, verifyCode);
        reqMap.put(RequestHeader.VIDEO_SOURCE, channel);
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_VALIDATE_CODES, reqMap, true);
    }

    /**
     *  完善资料
     * @param user
     */
    public void makeUserInfo(UserBean user, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.USER_INFO, new Gson().toJson(user));
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_SENDCODES3, reqMap, true);
    }
    /**
     *  绑定手机
     * @param phone
     * @param password
     */
    public void bindPhone(String phone,String password, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE,phone);
        try
        {
            reqMap.put(RequestHeader.PASSWORD, EncryptSHA1.SHA1(password));
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_MODIFYMOBILE, reqMap, true);
    }
}
